package org.picommo.tests;

import static org.junit.Assert.*;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import org.junit.Before;
import org.junit.Test;
import org.picommo.common.CellType;
import org.picommo.common.CellTypeList;
import org.picommo.common.Color;
import org.picommo.common.Direction;
import org.picommo.common.Frame;
import org.picommo.common.Vec2;
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.load.Persister;

public class SerializationTests {
	
	Serializer serializer;
	
	@Before
	public void setup() {
		serializer = new Persister();
	}
	
	
	@Test
	public void valueTypesSerialization() throws Exception {

		Frame f = new Frame(new Vec2(1,2), Direction.none);
		serializer.write(f, new File("test.xml"));
		Frame readFrame = serializer.read(Frame.class, new File("test.xml"));
		
		assertEquals(f, readFrame);
	}
	
	
	@Test
	public void cellTypeSerialization() throws Exception {
		
		final String testFilename = "test_celltypes.xml";
		
		List<CellType> cellTypes = new ArrayList<CellType>();
		cellTypes.add(new CellType("grass", true, new Color(0, 255, 0)));
		cellTypes.add(new CellType("water", false, new Color(0, 0, 255)));
		cellTypes.add(new CellType("forest", true, new Color(10, 100, 10)));
		cellTypes.add(new CellType("mountains", false, new Color(120, 120, 120)));
		cellTypes.add(new CellType("desert", true, new Color(255, 255, 0)));
		CellTypeList cellTypeList = new CellTypeList(cellTypes);
		serializer.write(cellTypeList, new File(testFilename));
		CellTypeList readList = serializer.read(CellTypeList.class, new File(testFilename));
		
		assertEquals(cellTypeList, readList);
	}

}
